package br.cs.correspodente.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.cs.correspodente.beans.Imovel;
import br.cs.correspodente.beans.Vendedor;
import br.cs.correspodente.conexao.FactoryConnection;
import br.cs.correspodente.interfaces.DAO;
import br.cs.correspodente.util.ImovelUtil;
import br.cs.correspodente.util.VendedorUtil;

public class ImovelDAO implements DAO<Imovel>{
	
	private final static String sqlAdd = "INSERT INTO Imovel VALUES(0,?,?,?,?);";
	private final static String sqlDell = "DELETE FROM Imovel WHERE idImovel = ?;";
	private final static String sqlUp = "UPDATE Imovel SET idFinanciamento=?, idVendedor=?,endereco=?,valor=? WHERE idImovel = ?;";
	private final static String sqlAll = "SELECT *FROM Imovel WHERE idImovel = ?;";
	private final static String sqlGetByID = "SELECT *FROM Imovel WHERE idImovel = ?;";
	
	@Override
	public void add(Imovel imovel) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlAdd);
//		stm.setInt(1, imovel.getIdFinancimanto());
//		stm.setInt(2, imovel.getIdVendendor());
		stm.setString(3, imovel.getEndereco());
		stm.setFloat(4, imovel.getValor());
		stm.execute();
		FactoryConnection.getInstance().closeConexao(con, stm);
		
	}

	@Override
	public void dell(int id) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlDell);
		stm.setInt(1,id);
		stm.execute();
		FactoryConnection.getInstance().closeConexao(con, stm);
		
	}

	@Override
	public void up(Imovel imovel) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlUp);
	//	stm.setInt(1, imovel.getIdFinancimanto());
	//	stm.setInt(2, imovel.getIdVendendor());
		stm.setString(3, imovel.getEndereco());
		stm.setFloat(4, imovel.getValor());
		stm.setInt(5,imovel.getId());	
		stm.executeUpdate();
		FactoryConnection.getInstance().closeConexao(con, stm);
				
	}

	@Override
	public ArrayList<Imovel> getAll() throws SQLException,
			ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlAll);
		ResultSet rs = stm.executeQuery();
		ArrayList<Imovel> imoveis = new ArrayList<Imovel>();
		while(rs.next()){
			imoveis.add(ImovelUtil.novoImovel(rs));
		}
		FactoryConnection.getInstance().closeConexao(con, stm);
		return imoveis;
	}

	@Override
	public Imovel getById(int id) throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement stm = con.prepareStatement(sqlGetByID);
		stm.setInt(1, id);
		ResultSet rs = stm.executeQuery();
		Imovel imovel = new Imovel();
		while(rs.next()){
			imovel = ImovelUtil.novoImovel(rs);
		}
		FactoryConnection.getInstance().closeConexao(con, stm);
		return imovel;
	}

}
